package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.dao.DetailDao;
import cn.sumpu.app.guoguojie.domain.Contact;
import cn.sumpu.app.guoguojie.domain.Detail;
import cn.sumpu.app.guoguojie.domain.Learning;
import cn.sumpu.app.guoguojie.domain.Personality;
import cn.sumpu.app.guoguojie.domain.Work;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class DetailDaoImpl extends SqlSessionDaoSupport implements DetailDao {

	private static Logger logger = LoggerFactory.getLogger(DetailDaoImpl.class);

	@Override
	public void insertDetail(Detail detail) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("cn.sumpu.app.guoguojie.persistence.DetailMapper.insertDetail",
							detail);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertDetail failure.", e);
			}
			throw new AppBusinessException("insertDetail failure.", e);
		}
	}
	
	@Override
	public Detail getDetailByMember(Map<String, Object> map) throws AppBusinessException {
		try {
			return (Detail) getSqlSession()
					.selectOne("cn.sumpu.app.guoguojie.persistence.DetailMapper.getDetailByMember",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDetailByMember failure.", e);
			}
			throw new AppBusinessException("getDetailByMember failure.", e);
		}
	}

	@Override
	public void updateDetail(Detail detail) throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.DetailMapper.updateDetail",
							detail);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateDetail failure.", e);
			}
			throw new AppBusinessException("updateDetail failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Detail> getDetailList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.DetailMapper.getDetailList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDetailList failure.", e);
			}
			throw new AppBusinessException("getDetailList failure.", e);
		}
	}

	@Override
	public Integer getDetailListCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.DetailMapper.getDetailListCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDetailListCount failure.", e);
			}
			throw new AppBusinessException("getDetailListCount failure.", e);
		}
	}

	//根据会员id查询个性化资料
	@SuppressWarnings("unchecked")
	@Override
	public List<Personality> getPersonalityList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.DetailMapper.getPersonalityList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getPersonalityList failure.", e);
			}
			throw new AppBusinessException("getPersonalityList failure.", e);
		}
	}
	
	//根据会员id查询工作资料
	@SuppressWarnings("unchecked")
	@Override
	public List<Work> getWorkList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.DetailMapper.getWorkList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getWorkList failure.", e);
			}
			throw new AppBusinessException("getWorkList failure.", e);
		}
	}
	
	//根据会员id查询学习资料
	@SuppressWarnings("unchecked")
	@Override
	public List<Learning> getLearningList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.DetailMapper.getLearningList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getLearningList failure.", e);
			}
			throw new AppBusinessException("getLearningList failure.", e);
		}
	}

	@Override
	public void deletePersonality(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.DetailMapper.deletePersonality",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deletePersonality failure.", e);
			}
			throw new AppBusinessException("deletePersonality failure.", e);
		}
	}

	@Override
	public void insertPersonality(Personality personality)
			throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.DetailMapper.insertPersonality",
							personality);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertPersonality failure.", e);
			}
			throw new AppBusinessException("insertPersonality failure.", e);
		}
	}
	
	@Override
	public void updatePersonality(Personality personality)
			throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.DetailMapper.updatePersonality",
							personality);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updatePersonality failure.", e);
			}
			throw new AppBusinessException("updatePersonality failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Contact> getContactList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.DetailMapper.getContactList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getContactList failure.", e);
			}
			throw new AppBusinessException("getContactList failure.", e);
		}
	}

	@Override
	public void deleteContact(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.DetailMapper.deleteContact",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteContact failure.", e);
			}
			throw new AppBusinessException("deleteContact failure.", e);
		}
	}

	@Override
	public void insertContact(Contact contact) throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.DetailMapper.insertContact",
							contact);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertContact failure.", e);
			}
			throw new AppBusinessException("insertContact failure.", e);
		}
	}

}
